package com.xmkj.bearbiz.member;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.util.SparseArray;
import android.view.View;
import android.widget.TextView;

import com.common.BearMethods.MemberInfoModifyMethods;
import com.common.cons.Constans;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.rxbus.RxBus;
import com.common.rxbus.postevent.RxKeyEvent;
import com.common.utils.EmptyUtils;
import com.common.utils.SPUtils;
import com.xmkj.bearbiz.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.functions.Action1;

public class ModifyPersonInfoAcitivity extends BaseMvpActivity {
    public static final String DEPTCODE = "DEPTCODE";
    public static final String VIPCARDID = "VIPCARDID";
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    SparseArray<String> titles = new SparseArray<>();
    @BindView(R.id.tv_cancle)
    TextView tvCancle;
    @BindView(R.id.tv_save)
    TextView tvSave;
    private String deptcode;
    private String vipcardid;

    private SaveUserInfoBean submitBean = new SaveUserInfoBean();

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_modify_persono_info;
    }

    @Override
    protected void onViewCreated() {

    }

    @Override
    protected void doLogicFunc() {
        deptcode = getIntent().getStringExtra(DEPTCODE);
        vipcardid = getIntent().getStringExtra(VIPCARDID);
        initTab();
        attachClickListener(tvSave);
        attachClickListener(tvCancle);
        rxManager.add(RxBus.getDefault().toObservable(RxKeyEvent.class).subscribe(new Action1<RxKeyEvent>() {
            @Override
            public void call(RxKeyEvent rxKeyEvent) {
                if (EmptyUtils.isNotEventEmpty(rxKeyEvent, RxKeyEvent.SAVE_FROM_BASE_INFO)) {
                    SaveUserInfoBean baseinfo = (SaveUserInfoBean) rxKeyEvent.getValue();
                    submitBean.vipcardid = baseinfo.vipcardid;
                    submitBean.deptcode = baseinfo.deptcode;
                    submitBean.customer_identity_ids = baseinfo.customer_identity_ids;
                    submitBean.occupation = baseinfo.occupation;
                    submitBean.consultant = baseinfo.consultant;
                    submitBean.birthday = baseinfo.birthday;
                    submitBean.idcard = baseinfo.idcard;
                    submitBean.home_address = baseinfo.home_address;
                    submitBean.company_name = baseinfo.company_name;
                    submitBean.faith = baseinfo.faith;
                    submitBean.family_festival = baseinfo.family_festival;
                    submitBean.remarks = baseinfo.remarks;

                }
            }
        }));
        rxManager.add(RxBus.getDefault().toObservable(RxKeyEvent.class).subscribe(new Action1<RxKeyEvent>() {
            @Override
            public void call(RxKeyEvent rxKeyEvent) {
                if (EmptyUtils.isNotEventEmpty(rxKeyEvent, RxKeyEvent.SAVE_FROM_LIFE_STYLE)) {
                    SaveUserInfoBean lifeStyle = (SaveUserInfoBean) rxKeyEvent.getValue();
                    submitBean.vipcardid = lifeStyle.vipcardid;
                    submitBean.deptcode = lifeStyle.deptcode;
                    submitBean.education = lifeStyle.education;
                    submitBean.vehicle = lifeStyle.vehicle;
                    submitBean.language = lifeStyle.language;
                    submitBean.like_topic = lifeStyle.like_topic;
                    submitBean.taboo_topic = lifeStyle.taboo_topic;
                    submitBean.like_smoke = lifeStyle.like_smoke;
                    submitBean.food_preference = lifeStyle.food_preference;
                    submitBean.family_information = lifeStyle.family_information;
                }
            }
        }));

        rxManager.add(RxBus.getDefault().toObservable(RxKeyEvent.class).subscribe(new Action1<RxKeyEvent>() {
            @Override
            public void call(RxKeyEvent rxKeyEvent) {
                if (EmptyUtils.isNotEventEmpty(rxKeyEvent, RxKeyEvent.SAVE_FROM_DRESSSTYLE_INFO)) {
                    SaveUserInfoBean dressStyle = (SaveUserInfoBean) rxKeyEvent.getValue();
                    submitBean.vipcardid = dressStyle.vipcardid;
                    submitBean.deptcode = dressStyle.deptcode;
                    submitBean.height = dressStyle.height;
                    submitBean.figure = dressStyle.figure;
                    submitBean.complexion = dressStyle.complexion;
                    submitBean.jacket_size = dressStyle.jacket_size;
                    submitBean.pants_size = dressStyle.pants_size;
                    submitBean.shoes_size = dressStyle.shoes_size;
                    submitBean.pants_length = dressStyle.pants_length;
                    submitBean.like_color = dressStyle.like_color;
                    submitBean.like_brand = dressStyle.like_brand;
                    submitBean.waistline = dressStyle.waistline;
                    submitBean.circumference = dressStyle.circumference;
                }
            }
        }));
    }

    @Override
    protected void onViewClicked(View view) {
        if (view.getId() == tvSave.getId()) {
            RxBus.getDefault().postSticky(new RxKeyEvent(RxKeyEvent.SAVE_USER_INFO, true));
            System.out.println("bean" + submitBean);
            getSubmit();
        } else if (view.getId() == tvCancle.getId()) {
            onBackPressed();
        }
    }

    private void getSubmit() {
        showProgressingDialog();
        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                showToastMsg("修改成功");
                onBackPressed();

            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });

//        jpushid = JPushInterface.getRegistrationID(this);
        MemberInfoModifyMethods.getInstance().editVipUser(subscriber, SPUtils.getShareInt(Constans.UID) + "", SPUtils.getShareString(Constans.HASHID),
                vipcardid,
                deptcode,
                submitBean.customer_identity_ids,
                submitBean.birthday,
                submitBean.occupation,
                submitBean.idcard,
                submitBean.home_address,
                submitBean.company_name,
                submitBean.faith,
                submitBean.family_festival,
                submitBean.remarks,
                submitBean.education,
                submitBean.language,
                submitBean.vehicle,
                submitBean.like_topic,
                submitBean.taboo_topic,
                submitBean.like_smoke,
                submitBean.food_preference,
                submitBean.family_information,
                submitBean.height,
                submitBean.figure,
                submitBean.complexion,
                submitBean.jacket_size,
                submitBean.pants_size,
                submitBean.shoes_size,
                submitBean.pants_length,
                submitBean.like_color,
                submitBean.like_brand,
                submitBean.waistline,
                submitBean.circumference,
                submitBean.consultant,
                submitBean.return_date
        );
        rxManager.add(subscriber);
    }

    @Override
    protected void setNavigation() {
        setNavigationBack("修改个人资料");
    }

    @Override
    public void bindKnife() {
        ButterKnife.bind(this);

    }

    private void initTab() {
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        titles.append(0, "基本资料");
        titles.append(1, "生活属性");
        titles.append(2, "穿衣属性");
        viewPager.setAdapter(new ModifyPagerAdapter(getSupportFragmentManager(), titles, context, deptcode, vipcardid));
        tabLayout.setupWithViewPager(viewPager);
    }


}
